<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.text.ParseException"%>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.*"%>
<%@page import="java.sql.*"%>
<%@page import="java.io.*"%>
<%@ page import="com.ucap.migration.*,com.ucap.utils.UUIDGenerator" %>
<%@page import="com.ucap.base.model.wcm.Manuscript"%>
<%@ page import="com.ucap.base.exception.wcm.WebContentManagementException"%>
<%@ page import="com.ucap.base.model.wcm.Manuscript"%>
<%@ page import="com.ucap.rpc.RpcUtils"  %>
<%@ page import="com.ucap.wcm.ManuscriptService,com.ucap.wcm.model.ManuscriptAllForTemplate" %>
<%@ page import="com.ucap.repository.RepositoryService"  %>
<%@ page import="com.ucap.repository.model.ResourceInfoWithByte"  %>
<%@ page import="com.ucap.wcm.model.ExtendedManuscriptObject"  %>
<%@page import="javax.naming.Context"%>
<%@page import="javax.naming.InitialContext"%>

<%
	final String channelId = "215856b2cd3046b7ada0b9b018568d9d";//临时栏目ID
// 	String SYH_ = "";//生成规则
	final String website = "9bbbb07c1d62450297ca9cfef795e523";
	final String pub = "0"; //稿件
	//标题
	String infotitle = getParameter(request,"title");
	//内容
	String content = getParameter(request,"content");
	//生成 时间
	String datetime = getParameter(request,"datetime");
	//文号
	String wh = getParameter(request,"wh");
	//体裁分类
	String tc = "97c3c1f912274e3e9d9b9687d8a65b59";//getParameter(request,"tc");
	
	//体裁分类
	String zt = "edb88100ca634170927650e78f0d0e3e";
	//文号
	String orgcode = getParameter(request,"orgcode");
	//发布机构(部门)
	String deptName = getParameter(request,"deptName");//部门怎么把两个系统关联起来
	//索引号
	String syhtype = getParameter(request,"syh");//request.getParameter("syh")==null?"":request.getParameter("syh");
	
	
	//判断非空
	if(nullOrEmpty(infotitle) || nullOrEmpty(content) ||nullOrEmpty(datetime) ||nullOrEmpty(wh)
	        ||nullOrEmpty(orgcode)){
	    out.print("失败");
	    return;
	}
	
	//声明对象
	com.ucap.base.model.wcm.Manuscript manuscript = null;
	try{
		manuscript = new com.ucap.base.model.wcm.Manuscript();
		//获取文章Id
		String ManuscriptId  = generateString32();
		manuscript.setManuscriptId(ManuscriptId);
		manuscript.setChannelId(channelId);
		manuscript.setWebsiteId(website);
		//信息公开的发布机构
		String info_fbjg ="" ;//rs.getString("info_expstr13")==null?"":rs.getString("info_expstr13");	
		
		if(infotitle!=null&&!"".equals(infotitle)){
			manuscript.setTitle(infotitle);// 去掉title空格
			manuscript.setSubTitle(infotitle);
		}else{
			manuscript.setTitle("");// 去掉title空格
		}				
		String info_subtitle = infotitle;	//						
		manuscript.setType("0");// 这里的type为1时可能出现稿件生成静态页正文出不来，显示为图片
		manuscript.setStatus("1");
		manuscript.setKeyword("");
		
		String dir = "";//publishedtime
		
		Date datetimes = toDate("yyyy-MM-dd hh:mm:ss",datetime);
		
		long pubdate = datetimes.getTime();//rs.getTimestamp("info_publishedtime").getTime();//发布时间
		pubdate = pubdate-28800000l;				
		manuscript.setSeqNum(pubdate);				
		manuscript.setPublishedTime(new Long(pubdate));
		manuscript.setCreatedTime(new Long(pubdate));
		manuscript.setSortedTime(new Long(pubdate));		
		manuscript.setModifiedTime(new Long(pubdate));
			
		dir = fotmatTime(datetimes,"yyyyMMdd");//rs.getString("dir");	//取发布日期的年月   去掉中间空格  比如 2013-10-17，那么dir就是201310		
		manuscript.setIsDisplayContent("0");
		manuscript.setIsSticky("0");				
		manuscript.setCreatorId("9999999999999999");			
	       	manuscript.setIsModifiedSeqNum("0");			
		manuscript.setIsEncrypted("0");
		manuscript.setIsPageBreak("0");	
		
		manuscript.setSortedTime(0l);
		manuscript.setIsEncrypted("0");
		manuscript.setIsPageBreak("0");
		manuscript.setSeqNum(System.currentTimeMillis());
		manuscript.setModifiedTime(System.currentTimeMillis());
		
		String memo = "摘要";
		manuscript.setMemo(memo);
		manuscript.setUrl(dir);
		
		if (!manuscript.getUrl().endsWith(".shtml")) {// 新增稿件
			manuscript.setUrl(manuscript.getUrl() + "/"
					+ ManuscriptId + ".shtml");// 生成稿件URL
		}
		ExtendedManuscriptObject object = new ExtendedManuscriptObject();
		Map map = new HashMap();
		map.put("MANUSCRIPT_INPUT_EXTENDEDMANUSCRIPT_FILENAME", "html");// 设置稿件类型
		map.put("channelId", channelId);// 栏目编号
		map.put("manuscripts_submit", false);// 是否提交
		map.put("content", content);// 稿件内容
		map.put("manuscript_id", manuscript.getManuscriptId());// 稿件编号
		object.setManuscriptAttachmentItem(map);
		object.setManuscript(manuscript);
		try {
			// 写入目的数据库
// 			final String URL = "http://xxgkhtgl.xz.gov.cn/website-webapp/services/";
// 			RepositoryService repositoryService =RpcUtils.findService(URL+"RepositoryService", RepositoryService.class);
// 			ManuscriptService manuService = RpcUtils.findService(URL+"ManuscriptService", ManuscriptService.class);
// 			System.out.println("【输出打印】开始调用CMS执行新增稿件");
// 			String mid = manuService.saveManuscriptForNoStreamWithId(object,manuscript.getCreatorId(), true);//稿件
			com.ucap.migration.manuscript.ManuscriptService manuscriptService = new com.ucap.migration.manuscript.ManuscriptService();
			com.ucap.migration.resource.ResourceTextHandle resourceTextHandle = new com.ucap.migration.resource.ResourceTextHandle();
			resourceTextHandle.saveText(manuscript, content);
			//out.println("********************"+content);	
			boolean result = manuscriptService.saveManuscript(manuscript);
// 			System.out.println("【输出打印】调用CMS新增稿件返回ID：" + mid);	
			if (result)
			{
				out.print("成功");
			}else{
			    out.print("失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.print("失败"+e.getLocalizedMessage());
		}
	}catch(Exception e){
		e.printStackTrace();
		out.print("失败"+e.getLocalizedMessage());
	}
%>
<%--一下为私有方法 --%>
<%!
private String getParameter(HttpServletRequest request,String name){
    String val = request.getParameter(name);
    if(null!=val && !"".equals(val)){
        return val;
    }
    return "";
} 

private boolean nullOrEmpty(String str){
    if(null!=str && !"".equals(str)){
        return false;
    }
    return true;
}

private Date toDate(String format, String strDate) throws ParseException {
	SimpleDateFormat sdf = new SimpleDateFormat(format);
	return sdf.parse(strDate);
}


public String fotmatTime(Date d,String format) {
	SimpleDateFormat sdf = new SimpleDateFormat(format);
	return sdf.format(d);
}

%>